﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using CustomControlsNS;

namespace NS_Controls
{
	public class ExtendedGridView : BulkEditGridView
	{
		protected int m_nInitialPageSize = -10;
		protected LinkButton m_lbPageSize;
		protected LinkButton m_lbFilter;
		protected LinkButton m_lbEdit;
		protected LinkButton m_lbInsert;
		protected TextBox m_txtPageSize;

		protected HtmlInputButton m_ButtonAllPages_PageSize;
		protected HtmlInputButton m_ResetButton_PageSize;
		protected Button m_OkButton_PageSize;
		protected Button m_CancelButton_PageSize;
		protected HtmlInputButton m_ResetButton_Filter;
		protected Button m_OkButton_Filter;
		protected Button m_CancelButton_Filter;

		protected HtmlInputButton m_ButtonAllPages_PageSize_;
		protected HtmlInputButton m_ResetButton_PageSize_;
		protected HtmlInputButton m_OkButton_PageSize_;
		protected HtmlInputButton m_CancelButton_PageSize_;
		protected HtmlInputButton m_ButtonAllPages_Filter_;
		protected HtmlInputButton m_ResetButton_Filter_;
		protected HtmlInputButton m_OkButton_Filter_;
		protected HtmlInputButton m_CancelButton_Filter_;

		#region ViewState and Session storage
		public bool TranslateColumnHeaders
		{
			get { return (bool)(this.ViewState["TranslateColumnHeaders"] ?? false); }
			set { this.ViewState["TranslateColumnHeaders"] = value; }
		}

		public string[] TranslateColumns
		{
			get { return (string[])(this.ViewState["TranslateColumns"] ?? new string[] { }); }
			set { this.ViewState["TranslateColumns"] = value; }
		}

		public string TotalColumns
		{
			get { return (string)(this.ViewState["TotalColumns"] ?? ""); }
			set
			{
				this.ViewState["TotalColumns"] = value;
				this.ShowFooter = this.TotalOnAllDecimalColumns || !string.IsNullOrEmpty(this.TotalColumns);
			}
		}

		public bool TotalOnAllDecimalColumns
		{
			get { return (bool)(this.ViewState["TotalOnAllDecimalColumns"] ?? false); }
			set
			{
				this.ViewState["TotalOnAllDecimalColumns"] = value;
				this.ShowFooter = this.TotalOnAllDecimalColumns || !string.IsNullOrEmpty(this.TotalColumns);
			}
		}

		public string[] ShowColumns
		{
			get { return (string[])(this.ViewState["ShowColumns"] ?? null); }
			set { this.ViewState["ShowColumns"] = value; }
		}

		public string[] HiddenColumns
		{
			get { return (string[])(this.Page.Session[this.ClientID + "_HiddenColumns"] ?? new string[] { }); }
			set { this.Page.Session[this.ClientID + "_HiddenColumns"] = value; }
		}

		public string[] ReturnColumns
		{
			get { return (string[])(this.ViewState["ReturnColumns"] ?? new string[] { }); }
			set { this.ViewState["ReturnColumns"] = value; }
		}

		public string[] DropDownColumns
		{
			get { return (string[])(this.ViewState["DropDownColumns"] ?? new string[] { }); }
			set { this.ViewState["DropDownColumns"] = value; }
		}

		public string[][] DropDownArguments
		{
			get { return (string[][])(this.ViewState["DropDownArguments"] ?? new string[][] { new string[] { } }); }
			set { this.ViewState["DropDownArguments"] = value; this.RequiresDataBinding = true; }
		}

		public string[] DateTimeColumn
		{
			get { return (string[])(this.ViewState["DateTimeColumn"] ?? new string[] { }); }
			set { this.ViewState["DateTimeColumn"] = value; }
		}

		public string DateTimeFormat
		{
			get { return (string)(this.ViewState["DateTimeFormat"] ?? "yyyy-MM-dd HH:mm:ss"); }
			set { this.ViewState["DateTimeFormat"] = value; }
		}

		public string[] LinkColumn
		{
			get { return (string[])(this.ViewState["LinkColumn"] ?? new string[] { }); }
			set { this.ViewState["LinkColumn"] = value; }
		}

		public string[] LinkFormat
		{
			get { return (string[])(this.ViewState["LinkFormat"] ?? new string[] { }); }
			set { this.ViewState["LinkFormat"] = value; }
		}

        public string[] LinkFields
        {
            get { return (string[])(this.ViewState["LinkFields"] ?? new string[] { }); }
            set { this.ViewState["LinkFields"] = value; }
        }

		public string[] LinkInNewWindow
		{
			get { return (string[])(this.ViewState["LinkInNewWindow"] ?? new string[] { }); }
			set { this.ViewState["LinkInNewWindow"] = value; }
		}

        public string[] LiteralColumns
        {
            get { return (string[])(this.ViewState["LiteralColumns"] ?? new string[] { }); }
            set { this.ViewState["LiteralColumns"] = value; }
        }

		public bool UseDefaultStyle
		{
			get { return (bool)(this.ViewState["UseDefaultStyle"] ?? false); }
			set { this.ViewState["UseDefaultStyle"] = value; }
		}

		public bool EnableOtherCriterias
		{
			get { return (bool)(this.ViewState["EnableOtherCriterias"] ?? false); }
			set { this.ViewState["EnableOtherCriterias"] = value; this.RequiresDataBinding = true; }
		}

		private Hashtable HtFilter
		{
			get { return (Hashtable)(this.ViewState["htFilter"] ?? new Hashtable()); }
			set { this.ViewState["htFilter"] = value; }
		}

		public bool EnableFilter
		{
			get { return (bool)(this.ViewState["EnableFilter"] ?? false); }
			set { this.ViewState["EnableFilter"] = value; this.RequiresDataBinding = true; }
		}

		public bool ShowEditLink
		{
			get { return (bool)(this.ViewState["ShowEditLink"] ?? true); }
			set { this.ViewState["ShowEditLink"] = value; this.RequiresDataBinding = true; }
		}

		public bool ShowInsertLink
		{
			get { return (bool)(this.ViewState["ShowInsertLink"] ?? true); }
			set { this.ViewState["ShowInsertLink"] = value; this.RequiresDataBinding = true; }
		}

		public bool EnablePageSize
		{
			get { return (bool)(this.ViewState["EnablePageSize"] ?? false); }
			set { this.ViewState["EnablePageSize"] = value; this.RequiresDataBinding = true; }
		}

		public bool EnableExport
		{
			get { return (bool)(this.ViewState["EnableExport"] ?? false); }
			set { this.ViewState["EnableExport"] = value; this.RequiresDataBinding = true; }
		}

		public bool EnableExportPDF
		{
			get { return (bool)(this.ViewState["EnableExportPDF"] ?? false); }
			set { this.ViewState["EnableExportPDF"] = value; this.RequiresDataBinding = true; }
		}

		public override int PageSize
		{
			get { return (int)(this.ViewState["PageSize"] ?? 10); }
			set
			{
				this.ViewState["PageSize"] = value;
				if (m_nInitialPageSize < 0 && value > 0)
					m_nInitialPageSize = value;
				this.RequiresDataBinding = true;
			}
		}

		private int Default_SessionKeyColumnHeadersIndex = -1;
		public int SessionKeyColumnHeadersIndex
		{// Default: Tables[0] - tabela cu date; Tables[1] - tabela cu corespondenta numelor coloanelor
			get
			{
				string sessionKey = this.ClientID + "_SessionKeyColumnHeadersIndex";
				if (this.Page.Session[sessionKey] == null)
					this.Page.Session[sessionKey] = Default_SessionKeyColumnHeadersIndex;	// Pentru ca in XLS sa ajunga o valoare
				return (int)(this.Page.Session[sessionKey] ?? Default_SessionKeyColumnHeadersIndex);
			}
			set { this.Page.Session[this.ClientID + "_SessionKeyColumnHeadersIndex"] = value; }
		}

		public string CaptionForXLS
		{
			get
			{
				string sessionKey = this.ClientID + "_CaptionForXLS";
				if (this.Page.Session[sessionKey] == null)
					this.Page.Session[sessionKey] = "";					// Pentru ca in XLS sa ajunga o valoare
				return (string)(this.Page.Session[sessionKey] ?? "");
			}
			set { this.Page.Session[this.ClientID + "_CaptionForXLS"] = value; }
		}

		public string GridViewSortDirection
		{
			get { return (string)this.Page.Session[this.ClientID + "_SortDirection"] ?? "DESC"; }
			set { this.Page.Session[this.ClientID + "_SortDirection"] = value; }
		}

		public string GridViewSortExpression
		{
			get { return (string)this.Page.Session[this.ClientID + "_SortExpression"] ?? ""; }
			set { this.Page.Session[this.ClientID + "_SortExpression"] = value; }
		}

		private string GridViewRowFilter
		{
			get { return (string)this.Page.Session[this.ClientID + "_RowFilter"] ?? ""; }
			set { this.Page.Session[this.ClientID + "_RowFilter"] = value; }
		}

		private string GridViewRowFilterText
		{
			get { return (string)this.Page.Session[this.ClientID + "_RowFilterText"] ?? ""; }
			set { this.Page.Session[this.ClientID + "_RowFilterText"] = value; }
		}

		public string SelectionClientID
		{
			get { return (string)(this.ViewState["SelectionClientID"] ?? ""); }
			set { this.ViewState["SelectionClientID"] = value; }
		}

		public string Skin
		{
			get { return (string)ViewState["Skin"] ?? "PET"; }
			set
			{
				if (value.ToUpper() == "OMV")
					value = "OMV";
				else if (value.ToUpper() == "PETROM")
					value = "PET";
				else
					value = "PET";
				ViewState["Skin"] = value.ToUpper();
			}
		}
		#endregion

		private string GetSortDirection()
		{
			switch (GridViewSortDirection)
			{
				case "ASC":
					GridViewSortDirection = "DESC";
					break;
				case "DESC":
					GridViewSortDirection = "ASC";
					break;
			}
			return GridViewSortDirection;
		}

		public string GetRowFilter()
		{
			ArrayList rowFiltersList = new ArrayList();
			if (this.GridViewRowFilter != "")
				rowFiltersList.Add(this.GridViewRowFilter);
			if (this.SessionKeyFilter != "")
				rowFiltersList.Add(this.SessionKeyFilter);
			string[] rowFiltersArray = new string[rowFiltersList.Count];
			rowFiltersList.CopyTo(rowFiltersArray);
			string s = string.Join(") AND (", rowFiltersArray);
			if (rowFiltersArray.Length > 1)
				s = "(" + s + ")";
			return s;
		}

		private DataView GetDataSource(bool isPageIndexChanging)
		{
			if (this.DataSource != null)
			{
				DataView dataView = this.DataSource;
				if (GridViewSortExpression != "")
					dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, (isPageIndexChanging ? GridViewSortDirection : GetSortDirection()));
				else
					dataView.Sort = "";
				dataView.RowFilter = GetRowFilter();

				return dataView;
			}
			else
				return null;
		}

		public new DataView DataSource
		{
			get
			{
				DataView dv = (DataView)base.DataSource;
				if (dv != null)
				{
					if (GridViewSortExpression != "")
						dv.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
					else
						dv.Sort = "";
				}
				if (dv != null)
					dv.RowFilter = GetRowFilter();
				return dv;
			}
			set
			{
				base.DataSource = value;
				m_Fields = null;
				if (DataSourceChanged != null)
					DataSourceChanged(this, null);
			}
		}
		public event EventHandler DataSourceChanged;

		protected override void OnPageIndexChanging(GridViewPageEventArgs e)
		{
			this.DataSource = GetDataSource(true);
			this.PageIndex = e.NewPageIndex;
			this.DataBind();
		}

		protected override void OnSorting(GridViewSortEventArgs e)
		{
			GridViewSortExpression = e.SortExpression;
			int pageIndex = this.PageIndex;
			this.DataSource = GetDataSource(false);
			this.DataBind();
			this.PageIndex = pageIndex;
		}

		protected override void OnPageIndexChanged(EventArgs e)
		{
			if (this.BottomPagerRow != null && this.DataSource != null)
			{
				this.BottomPagerRow.Cells[0].Text = Resources.GridView.Page + " " + (this.PageIndex + 1).ToString() + " " + Resources.GridView.From2 + " " + this.PageCount.ToString() + ". (" + this.DataSource.Count.ToString() + " " + Resources.GridView.Lines + ")";
			}

			base.OnPageIndexChanged(e);
		}

		protected override void OnInit(EventArgs e)
		{
			if (!this.Page.IsPostBack)
			{
				SessionKeyColumnHeadersIndex = Default_SessionKeyColumnHeadersIndex;
				CaptionForXLS = null;
				GridViewSortDirection = null;
				GridViewSortExpression = null;
				GridViewRowFilter = null;
				GridViewRowFilterText = null;
			}

			base.OnInit(e);

			if (this.UseDefaultStyle)
			{
				this.BackColor = Color.White;
				this.BorderColor = Color.FromName("#E5EAEA");
				this.BorderStyle = BorderStyle.Solid;
				this.BorderWidth = Unit.Pixel(1);
				this.CellPadding = 3;
				this.GridLines = GridLines.Both;

				this.PagerSettings.Mode = PagerButtons.NumericFirstLast;
				this.PagerSettings.FirstPageText = Resources.GridView.FirstPageText;
				this.PagerSettings.LastPageText = Resources.GridView.LastPageText;
				this.PagerSettings.NextPageText = Resources.GridView.NextPageText;
				this.PagerSettings.PreviousPageText = Resources.GridView.PreviousPageText;

				this.RowStyle.CssClass = "RowStyleClass";
				this.AlternatingRowStyle.CssClass = "AlternatingRowStyleClass";
				this.PagerStyle.CssClass = "PagerStyleClass";
				this.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
				this.SelectedRowStyle.CssClass = "SelectedRowStyleClass";
				this.SelectedRowStyle.Font.Bold = true;
				this.HeaderStyle.CssClass = "HeaderStyleClass";
				this.HeaderStyle.Font.Bold = true;
				this.FooterStyle.CssClass = "FooterStyleClass";

				AddLinkedStyleSheet(this.Page, "~/Styles/Grid.css");
				this.EmptyDataText = Resources.GridView.NoRecordFound;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			string s = @"
				function OnClick_ResetButton_Filter_Find(p, x)
				{
					if (p.id == null)
						return null;
					if (p.id.indexOf(x) != -1)
						return p;
					for (var i=0; i<p.childNodes.length; ++i)
					{
						var y = OnClick_ResetButton_Filter_Find(p.childNodes[i], x);
						if (y != null)
							return y;
					}
					return null;
				}
				function OnClick_ResetButton_Filter2(tbl)
				{
					for (var i=0; i<tbl.rows.length; ++i)
						for (var j=0; j<tbl.rows[0].cells.length; ++j)
							if (tbl.rows[i].cells.length > j && tbl.rows[i].cells[j].childNodes.length > 0 && tbl.rows[i].cells[j].childNodes[0] != null && tbl.rows[i].cells[j].childNodes[0].tagName == 'INPUT')
								tbl.rows[i].cells[j].childNodes[0].value = '';
				}
				function OnClick_ResetButton_Filter(r)
				{
					var p = $get(r).rows[$get(r).rows.length-1].cells[0];
					var x = OnClick_ResetButton_Filter_Find(p, '_tblFilter');
					OnClick_ResetButton_Filter2(x);
					x = OnClick_ResetButton_Filter_Find(p, '_tblFilter2');
					OnClick_ResetButton_Filter2(x);
					x = OnClick_ResetButton_Filter_Find(p, '_txtAlteCriterii');
					x.value = '';
				}
			";

			ScriptManager.RegisterStartupScript(this, typeof(Page), "ResetButton_Filter", s, true);

			Tools.AddLinkedStyleSheet(this.Page, "~/Styles/Grid.css");
			base.OnPreRender(e);
		}

		private void AddLinkedStyleSheet(Page page, string stylesheet)
		{
			bool bAdded = false;
			foreach (Control c in page.Header.Controls)
				if (c is HtmlLink && ((HtmlLink)c).Href == stylesheet)
				{
					bAdded = true;
					continue;
				}
			if (!bAdded)
			{
				HtmlLink link = new HtmlLink();
				link.Href = stylesheet;
				link.Attributes["rel"] = "stylesheet";
				link.Attributes["type"] = "text/css";

				page.Header.Controls.Add(link);
			}
		}

		private int GetHeaderNo()
		{
			int n = 0;

			DataTable dt = this.DataSource.Table;
			DataControlField[] fields = this.Fields;
			for (int i = 0; i < fields.Length; ++i)
			{
				int x = GetDataFieldHeaderName(GetDataField(i)).Split(new char[] { '$' }).Length;
				if (x > n)
					n = x;
			}

			return n;
		}

		private string GetDataField(int i)
		{
			DataControlField dcfc = this.Fields[i];
			return GetDataField(dcfc);
		}

		private string GetDataField(DataControlField dcfc)
		{
			string strDataField = "";

			if (dcfc is HyperLinkField)
				strDataField = ((HyperLinkField)dcfc).DataTextField;
			else if (dcfc is CheckBoxField)
				strDataField = ((CheckBoxField)dcfc).DataField;
			else if (dcfc is TextBoxField)
				strDataField = ((TextBoxField)dcfc).DataField;
			else if (dcfc is DropDownField)
				strDataField = ((DropDownField)dcfc).DataField;
			else if (dcfc is AutoGeneratedField)
				strDataField = ((AutoGeneratedField)dcfc).DataField;
			else if (dcfc is BoundField)
				strDataField = ((BoundField)dcfc).DataField;

			return strDataField;
		}

		private int GetColumnOrdinal(DataControlField[] fields, string s)
		{
			int n = -1;
			for (int i = 0; i < fields.Length; ++i)
			{
				if (GetDataField(i) == s)
				{
					n = i;
					break;
				}
			}
			return n;
		}

		private string GetDataFieldHeaderName(string strField)
		{
			if (this.SessionKeyColumnHeadersIndex > 0 && this.DataSource != null && this.DataSource.Table.DataSet.Tables.Count > this.SessionKeyColumnHeadersIndex && this.DataSource.Table.DataSet.Tables[this.SessionKeyColumnHeadersIndex].Columns.Contains("ColumnName_ID") && this.DataSource.Table.DataSet.Tables[this.SessionKeyColumnHeadersIndex].Columns.Contains("ColumnName"))
			{
				DataView dv = new DataView(this.DataSource.Table.DataSet.Tables[this.SessionKeyColumnHeadersIndex]);
				dv.RowFilter = "ColumnName_ID = '" + strField + "'";
				if (dv.Count > 0)
					strField = dv[0]["ColumnName"].ToString();
			}
			return strField;
		}

		private string GetDataFieldHeaderName2(string strField, bool bTranslate)
		{
			// Get ColumnHeader text from DB
			strField = GetDataFieldHeaderName(strField);
			// Translate ColumnHeaders
			if (bTranslate)
				strField = Tools.GetHeaderText(strField);
			return strField;
		}

		protected override AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)
		{
			AutoGeneratedField f = base.CreateAutoGeneratedColumn(fieldProperties);
			f.HeaderText = GetDataFieldHeaderName2(f.HeaderText, this.TranslateColumnHeaders);
			return f;
		}

		protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
		{
			ArrayList arFields = new ArrayList(base.CreateColumns(dataSource, useDataSource));

			for (int i = arFields.Count - 1; i >= 0; --i)
			{
				DataControlField dcfc = (DataControlField)arFields[i];
				string strDataField = GetDataField(dcfc);

				if (Tools.ArrayIndexOf(this.HiddenColumns, strDataField) != -1 && Tools.ArrayIndexOf(this.HiddenColumns, "") == -1)
					arFields.RemoveAt(i);

				if (!this.showDeleteButton)
				{
					if (dcfc.HeaderText == "D")
						arFields.RemoveAt(i);
				}

				if (!this.showEditButton)
				{
					if (dcfc.HeaderText == "E")
						arFields.RemoveAt(i);
				}

				int x = Tools.ArrayIndexOf(this.LinkColumn, strDataField );
				if (x != -1 && Tools.ArrayIndexOf(this.LinkColumn, "") == -1)
				{
					HyperLinkFieldX hf = new HyperLinkFieldX();
					hf.DataField = strDataField;
					hf.HeaderText = dcfc.HeaderText;
					hf.SortExpression = strDataField;
					hf.DataNavigateUrlFormatString = this.Context.Request.ApplicationPath + "/" + this.LinkFormat[x];
					if (this.LinkInNewWindow[x] == "1")
						hf.Target = "_self";
					hf.ControlStyle.CssClass = "aLinkGrid";
					arFields.RemoveAt(i);
					arFields.Insert(i, hf);
				}

				int y = Tools.ArrayIndexOf(this.DropDownColumns, strDataField);
				if (y != -1 && Tools.ArrayIndexOf(this.DropDownColumns, "") == -1)
				{
					DropDownField df = new DropDownField(y.ToString());
					df.HeaderText = strDataField;
					df.DataField = strDataField;
					df.ListDataSourceSessionDataSetKey = this.DropDownArguments[y][0];
					df.ListDataSourceSessionTableKey = this.DropDownArguments[y][1];
					df.ListDataValueField = this.DropDownArguments[y][2];
					df.ListDataTextField = this.DropDownArguments[y][3];
					df.SortExpression = strDataField;
					df.dataViewSource = (((DataSet)this.Page.Session[this.DropDownArguments[0][0]]).Tables[Convert.ToInt32(this.DropDownArguments[0][1])].DefaultView);
					arFields.RemoveAt(i);
					arFields.Insert(i, df);
				}

				int z = Tools.ArrayIndexOf(this.DateTimeColumn, strDataField);
				if (z != -1 && Tools.ArrayIndexOf(this.DateTimeColumn, "") == -1)
				{
					DateTimeField dtf = new DateTimeField();
					dtf.HeaderText = strDataField;
					dtf.DataField = strDataField;
					dtf.FullDateTime = false;
					dtf.DateTimeFormat = DateTimeFormat;
                    dtf.sufix = i.ToString();
					dtf.SortExpression = strDataField;
					arFields.RemoveAt(i);
					arFields.Insert(i, dtf);
				}

                int w = Tools.ArrayIndexOf(this.LiteralColumns, strDataField);
                if (w != -1 && Tools.ArrayIndexOf(this.LiteralColumns, "") == -1)
                {
                    LiteralField lit = new LiteralField();
                    lit.DataField = strDataField;
                    lit.HeaderText = dcfc.HeaderText;
                    lit.SortExpression = strDataField;
                    arFields.RemoveAt(i);
                    arFields.Insert(i, lit);
                }
			}

			return arFields;
		}

		protected override void OnRowCreated(GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header && this.DataSource != null)
			{
				int nHeaderNo = GetHeaderNo();
				DataControlField[] fields = this.Fields;

				for (int i = 0; i < nHeaderNo - 1; ++i)
				{
					GridViewRow header = this.CreateRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
					this.InitializeRow(header, fields);
					this.InnerTable.Rows.Add(header);
				}
			}

			if (e.Row.RowType == DataControlRowType.Pager && this.DataSource != null)
			{
				// Count visible columns
				int nVisibleColumns = 0;
				if (this.ShowColumns != null)
				{
					DataControlField[] fields = this.Fields;
					for (int i = 0; i < fields.Length; ++i)
						if (Tools.ArrayIndexOf(this.ShowColumns, GetDataField(i)) != -1)
							++nVisibleColumns;
				}
				else
					nVisibleColumns = this.Fields.Length;

				// Page number
				TableCell tc = new TableCell();
				tc.ColumnSpan = (nVisibleColumns == 1 ? 1 : (nVisibleColumns <= 3 ? nVisibleColumns - 1 : 3));
				tc.HorizontalAlign = HorizontalAlign.Left;
				e.Row.Cells.AddAt(0, tc);

				tc.Text = Resources.GridView.Page + " " + (this.PageIndex + 1).ToString() + " " + Resources.GridView.From2 + " " + this.PageCount.ToString() + ". (" + this.DataSource.Count.ToString() + " " + Resources.GridView.Lines + ")";
				foreach (TableCell td in e.Row.Cells)
					td.Wrap = false;
			}

			base.OnRowCreated(e);
		}

		protected override void OnRowDataBound(GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				if (this.InnerTable.Rows.Count > 1)
				{
					// Textele headerelor
					string[][] arHeaders = new string[this.InnerTable.Rows.Count][];
					// Numele field-urilor
					string[][] arHeaders2 = new string[this.InnerTable.Rows.Count][];
					// Desparte textul din header dupa caracterul $
					for (int i = 0; i < arHeaders.Length; ++i)
					{
						arHeaders[i] = new string[this.InnerTable.Rows[i].Cells.Count];
						arHeaders2[i] = new string[this.InnerTable.Rows[i].Cells.Count];
						for (int j = 0; j < arHeaders[i].Length; ++j)
						{
							string strDataField2 = GetDataField(j);
							string strDataField = GetDataFieldHeaderName2(strDataField2, true);
							arHeaders[i][j] = strDataField;
							string[] arDataField = strDataField.Split(new char[] { '$' });
							if (arDataField.Length > 1 && i < arDataField.Length)
								arHeaders[i][j] = arDataField[i];

							if (strDataField2.StartsWith("Column_"))
								strDataField2 = strDataField2.Substring(("Column_").Length);
							arHeaders2[i][j] = strDataField2;
							string[] arDataField2 = strDataField2.Split(new char[] { '$' });
							if (arDataField2.Length > 1 && i < arDataField2.Length)
								arHeaders2[i][j] = string.Join("$", arDataField2, 0, i + 1);
						}
					}
					// Seteaza textele headerelor
					for (int i = 0; i < this.InnerTable.Rows.Count; ++i)
						for (int j = 0; j < this.InnerTable.Rows[i].Cells.Count; ++j)
							if (this.InnerTable.Rows[i].Cells[j].Controls.Count == 0)
								this.InnerTable.Rows[i].Cells[j].Text = arHeaders[i][j];
							else
								if (this.InnerTable.Rows[i].Cells[j].Controls[0] is LinkButton)
									((LinkButton)this.InnerTable.Rows[i].Cells[j].Controls[0]).Text = arHeaders[i][j];

					// Uneste celulele de pe acelasi row care au aceeasi valoare
					for (int i = 0; i < this.InnerTable.Rows.Count; ++i)
						if (this.InnerTable.Rows[i].Cells.Count > 0)
						{
							int j = 0, k = 0;
							string x = arHeaders2[i][j];

							++j;
							while (j < this.InnerTable.Rows[i].Cells.Count)
							{
								string y = arHeaders2[i][j];

								if (x == y)
								{
									if (this.InnerTable.Rows[i].Cells[k].ColumnSpan == 0)
										this.InnerTable.Rows[i].Cells[k].ColumnSpan = 2;
									else
										this.InnerTable.Rows[i].Cells[k].ColumnSpan++;
									this.InnerTable.Rows[i].Cells[j].Visible = false;

									if (this.InnerTable.Rows[i].Cells[j].Controls.Count > 0 && this.InnerTable.Rows[i].Cells[j].Controls[0] is LinkButton && this.InnerTable.Rows[i].Cells[k].Controls.Count == 0)
										this.InnerTable.Rows[i].Cells[k].Controls.Add(this.InnerTable.Rows[i].Cells[j].Controls[0]);
								}
								else
								{
									k = j;
									x = y;
								}
								++j;
							}
						}
					// Uneste celulele de pe aceeasi coloana care au aceeasi valoare
					if (this.InnerTable.Rows.Count > 0 && this.InnerTable.Rows[0].Cells.Count > 0)
						for (int j = 0; j < this.InnerTable.Rows[0].Cells.Count; ++j)
						{
							int i = 0, k = 0;
							string x = arHeaders2[i][j];

							++i;
							while (i < this.InnerTable.Rows.Count)
							{
								string y = arHeaders2[i][j];

								if (x == y)
								{
									if (this.InnerTable.Rows[k].Cells[j].RowSpan == 0)
										this.InnerTable.Rows[k].Cells[j].RowSpan = 2;
									else
										this.InnerTable.Rows[k].Cells[j].RowSpan++;
									this.InnerTable.Rows[i].Cells[j].Visible = false;

									if (this.InnerTable.Rows[i].Cells[j].Controls.Count > 0 && this.InnerTable.Rows[i].Cells[j].Controls[0] is LinkButton && this.InnerTable.Rows[k].Cells[j].Controls.Count == 0)
										this.InnerTable.Rows[k].Cells[j].Controls.Add(this.InnerTable.Rows[i].Cells[j].Controls[0]);
								}
								else
								{
									k = i;
									x = y;
								}
								++i;
							}
						}
				}
				foreach (TableCell tc in e.Row.Cells)
					tc.Style.Add("border-bottom", "1px solid " + (this.Skin == "PET" ? "#FFEB00" : "#66CC00"));
			}

			if (this.ShowColumns != null && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.Footer))
			{
				DataControlField[] fields = this.Fields;
				for (int i = 0; i < fields.Length; ++i)
					if (Tools.ArrayIndexOf(this.ShowColumns, GetDataField(i)) == -1)
						e.Row.Cells[i].Visible = false;
			}
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				DataControlField[] fields = this.Fields;
				// RightAlign numbers
				for (int i = 0; i < fields.Length; ++i)
				{
					string strDataField = GetDataField(fields[i]);
					if (strDataField != "")
					{
						Type t = this.DataSource.Table.Columns[strDataField].DataType;
						if (t == Type.GetType("System.Decimal") || t == Type.GetType("System.Double") || t == Type.GetType("System.Float") || t == Type.GetType("System.Single"))
							e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
					}
				}
				if (this.ReturnColumns != null && this.ReturnColumns.Length != 0)
				{// Array with ReturnColumns
					string s = Tools.DoPackDataView((DataRowView)e.Row.DataItem, this.ReturnColumns, null, null, "', '");
					if (s != "")
						s = "'" + s + "'";
					s = "new Array(" + s + ")";
					foreach (TableCell td in e.Row.Cells)
					{
						td.Style.Add("cursor", "pointer");
						td.ToolTip = Resources.GridView.Select;
						td.Attributes["onclick"] = "onOk_Selection_" + this.SelectionClientID + "(" + s + ");";
					}
				}
				if (this.TranslateColumns != null && this.TranslateColumns.Length != 0)
				{// Translate Columns
					foreach (string s in this.TranslateColumns)
					{
						int n = GetColumnOrdinal(fields, s);
						if (n != -1)
						{
							if (e.Row.Cells[n].Controls.Count > 0 && e.Row.Cells[n].Controls[0] is HyperLink)
								((HyperLink)e.Row.Cells[n].Controls[0]).Text = Tools.GetHeaderText(((HyperLink)e.Row.Cells[n].Controls[0]).Text);
							else if (e.Row.Cells[n].Controls.Count > 0 && e.Row.Cells[n].Controls[0] is Label)
								((Label)e.Row.Cells[n].Controls[0]).Text = Tools.GetHeaderText(((Label)e.Row.Cells[n].Controls[0]).Text);
							else
								e.Row.Cells[n].Text = Tools.GetHeaderText(e.Row.Cells[n].Text);
						}
					}
				}
			}
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				if (((this.TotalColumns != null && this.TotalColumns != "") || this.TotalOnAllDecimalColumns) && this.DataSource != null)
				{// Totals
					DataTable dt = this.DataSource.Table;
					e.Row.Cells[0].Text = "<b>Total</b>";
					string[] arTotalColumns;
					if (!this.TotalOnAllDecimalColumns)
						arTotalColumns = this.TotalColumns.Split(new char[] { ',' });
					else
					{
						arTotalColumns = new string[dt.Columns.Count];
						for (int i = 0; i < dt.Columns.Count; ++i)
							if (dt.Columns[i].DataType == Type.GetType("System.Decimal") || dt.Columns[i].DataType == Type.GetType("System.Double") || dt.Columns[i].DataType == Type.GetType("System.Float") || dt.Columns[i].DataType == Type.GetType("System.Single"))
								arTotalColumns[i] = dt.Columns[i].ColumnName;
					}
					DataControlField[] fields = this.Fields;
					foreach (string s in arTotalColumns)
					{
						if (s == null || s == "")
							continue;

						decimal d = 0;
						object o = dt.Compute("SUM ([" + s + "])", this.GridViewRowFilter);
						if (o != System.DBNull.Value)
							d = Convert.ToDecimal(o);

						int n = GetColumnOrdinal(fields, s);
						if (n != -1)
						{
							string strFormat = "N";
							if (dt.Columns[s].DataType == Type.GetType("System.Int32") || dt.Columns[s].DataType == Type.GetType("System.Byte"))
								strFormat = "N0";
							e.Row.Cells[n].Text = "<b>" + d.ToString(strFormat) + "</b>";
							e.Row.Cells[n].HorizontalAlign = HorizontalAlign.Right;
							e.Row.Cells[n].Wrap = false;
						}
					}
				}
			}

			base.OnRowDataBound(e);
		}

		protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
		{
			int numRows = base.CreateChildControls(dataSource, dataBinding);

			if (dataSource != null)
			{
				DataControlField[] fields = null;
				GridViewRow row = null;

				bool needsFooterRow = false;
				needsFooterRow |= (EnablePageSize || EnableFilter || EnableExport || EnableExportPDF) && (Fields.Length > 0);
				needsFooterRow |= ShowEditLink || ShowInsertLink;
				if (needsFooterRow)
				{
					fields = this.Fields;
					// Creare row
					row = base.CreateRow(this.InnerTable.Rows.Count, -1, DataControlRowType.Pager, DataControlRowState.Normal);
					this.InitializeRow(row, fields);
					this.InnerTable.Rows.Add(row);
					if (fields.Length > 0)
					{
						row.Cells[0].ColumnSpan = row.Cells.Count;
						row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
						for (int i = 1; i < row.Cells[0].ColumnSpan; ++i)
							row.Cells.RemoveAt(1);
						row.Cells[0].Controls.Clear();
					}
				}
				else
					return numRows;

				if (this.EnablePageSize && fields.Length > 0)
				{
					// Creare controale ascunse
					LinkButton l21 = new LinkButton();
					l21.ID = "lbPageSize2";
					l21.Style.Add("display", "none");
					l21.Click += new EventHandler(OnClick_lbPageSize2);

					int q = this.InnerTable.Parent.Controls.IndexOf(this.InnerTable);
					this.InnerTable.Parent.Controls.AddAt(q + 1, l21);

					TextBox tt1 = new TextBox();
					tt1.ID = "txtPageSize2";
					tt1.Style.Add("display", "none");

					q = this.InnerTable.Parent.Controls.IndexOf(this.InnerTable);
					this.InnerTable.Parent.Controls.AddAt(q + 2, tt1);

					// Panel paginare
					Panel p1 = CreatePanelPaginare();
					row.Cells[0].Controls.Add(p1);
					m_ButtonAllPages_PageSize.Attributes["onclick"] = "$get('" + m_txtPageSize.ClientID + "').value = -1;";
					m_ResetButton_PageSize.Attributes["onclick"] = "$get('" + m_txtPageSize.ClientID + "').value = " + Math.Abs(m_nInitialPageSize) + ";";

					m_ButtonAllPages_PageSize_.Attributes["onclick"] = "$get('" + m_ButtonAllPages_PageSize.ClientID + "').click();";
					m_ResetButton_PageSize_.Attributes["onclick"] = "$get('" + m_ResetButton_PageSize.ClientID + "').click();";
					m_OkButton_PageSize_.Attributes["onclick"] = "$get('" + m_OkButton_PageSize.ClientID + "').click();";
					m_CancelButton_PageSize_.Attributes["onclick"] = "$get('" + m_CancelButton_PageSize.ClientID + "').click();";

					// Paginare
					Literal l1 = new Literal();
                    l1.Text = Resources.GridView.LinesPerPage + ": ";
					row.Cells[0].Controls.Add(l1);

					LinkButton l2 = new LinkButton();
					l2.ID = "lbPageSize";
					l2.CssClass = "aLinkGrid";
					l2.Text = (this.AllowPaging && this.PageSize > 0 ? this.PageSize.ToString() : Resources.GridView.AllLines);
					row.Cells[0].Controls.Add(l2);
					m_lbPageSize = l2;

					Literal l3 = new Literal();
					l3.Text = ". ";
					row.Cells[0].Controls.Add(l3);

					// Atlas popup paginare
					ModalPopupExtender mpe = new ModalPopupExtender();
					mpe.TargetControlID = l2.ID;
					mpe.PopupControlID = "pnPageSize";
					mpe.BackgroundCssClass = "modalBackground";
					//mpe.DropShadow = true;
					mpe.OkControlID = "OkButton_PageSize";
					mpe.CancelControlID = "CancelButton_PageSize";
					mpe.OnOkScript = "$get('" + tt1.ClientID + "').value = $get('" + m_txtPageSize.ClientID + "').value; __doPostBack('" + l21.UniqueID + "', '');";
					row.Cells[0].Controls.Add(mpe);
				}

				if (this.EnableFilter && fields.Length > 0)
				{
					LinkButton l22 = new LinkButton();
					l22.ID = "lbFilter2";
					l22.Style.Add("display", "none");
					l22.Click += new EventHandler(OnClick_lbFilter2);

					int q = this.InnerTable.Parent.Controls.IndexOf(this.InnerTable);
					this.InnerTable.Parent.Controls.AddAt(q + 1, l22);

					// Panel filtrare
					Panel p = CreatePanelFiltrare(fields);
					row.Cells[0].Controls.Add(p);
					m_ResetButton_Filter_.Attributes["onclick"] = "$get('" + m_ResetButton_Filter.ClientID + "').click();";
					m_OkButton_Filter_.Attributes["onclick"] = "$get('" + m_OkButton_Filter.ClientID + "').click();";
					m_CancelButton_Filter_.Attributes["onclick"] = "$get('" + m_CancelButton_Filter.ClientID + "').click();";

					// Filtrare
					Literal l4 = new Literal();
                    l4.Text = Resources.GridView.FilterCriterias + ": ";
					row.Cells[0].Controls.Add(l4);

					LinkButton l5 = new LinkButton();
					l5.ID = "lbFilter";
					l5.CssClass = "aLinkGrid";
                    l5.Text = "No Filter";//(this.GridViewRowFilterText == "" ? Resources.GridView.NoFilter : this.GridViewRowFilterText);
					switch (this.HtFilter.Count)
					{
						case 0: l5.Text = Resources.GridView.NoFilter; break;
						case 1: l5.Text = Resources.GridView.NrFiltre1; break;
						default: l5.Text = string.Format(Resources.GridView.NrFiltrePlural, this.HtFilter.Count); break;
					}
					row.Cells[0].Controls.Add(l5);
					m_lbFilter = l5;

					Literal l6 = new Literal();
					l6.Text = ". ";
					row.Cells[0].Controls.Add(l6);

					// Atlas popup paginare
					ModalPopupExtender mpe2 = new ModalPopupExtender();
					mpe2.TargetControlID = l5.ID;
					mpe2.PopupControlID = "pnFilter";
					mpe2.BackgroundCssClass = "modalBackground";
					//mpe2.DropShadow = true;
					mpe2.OkControlID = "OkButton_Filter";
					mpe2.CancelControlID = "CancelButton_Filter";
					mpe2.OnOkScript = "__doPostBack('" + l22.UniqueID + "', '');";
					row.Cells[0].Controls.Add(mpe2);
				}

				if (this.EnableExport && fields.Length > 0)
				{
					// Export Excel
					Literal l7 = new Literal();
					l7.Text = Resources.GridView.ExportTo + " ";
					row.Cells[0].Controls.Add(l7);

					HyperLink l8 = new HyperLink();
					l8.CssClass = "aLinkGrid";
					l8.Target = "_blank";
					l8.Text = "Excel";
					l8.NavigateUrl = this.Page.Request.ApplicationPath + "/Tools/ExportExcelHandler.ashx?s=" + this.SessionKey + "&c=" + this.ClientID;
					row.Cells[0].Controls.Add(l8);

					Literal l9 = new Literal();
					l9.Text = ". ";
					row.Cells[0].Controls.Add(l9);
				}

				if (this.ShowEditLink)
				{
					LinkButton l10 = new LinkButton();
					l10.ID = "lbEdit";
					l10.CssClass = "aLink";
					l10.Text = this.EnableEdit ? Resources.GridView.NotEdit : Resources.GridView.EditAll;
					l10.Click += new EventHandler(OnClick_lbEdit);
					row.Cells[0].Controls.Add(l10);
					m_lbEdit = l10;

					Literal l12 = new Literal();
					l12.Text = ". ";
					row.Cells[0].Controls.Add(l12);
				}

				if (this.ShowInsertLink)
				{
					LinkButton l11 = new LinkButton();
					l11.ID = "lbInsert";
					l11.CssClass = "aLink";
					l11.Text = this.EnableInsert ? Resources.GridView.NotInsert : Resources.GridView.Insert;
					l11.Click += new EventHandler(OnClick_lbInsert);
					row.Cells[0].Controls.Add(l11);
					m_lbInsert = l11;

					Literal l13 = new Literal();
					l13.Text = ". ";
					row.Cells[0].Controls.Add(l13);
				}

				if (this.EnableExportPDF && fields.Length > 0)
				{
					// Export PDF
					Literal l10 = new Literal();
					l10.Text = Resources.GridView.ExportTo + " ";
					row.Cells[0].Controls.Add(l10);

					HyperLink l11 = new HyperLink();
					l11.CssClass = "aLinkGrid";
					l11.Target = "_blank";
					l11.Text = "PDF";
					l11.NavigateUrl = this.Page.Request.ApplicationPath + "/Tools/ExportPDF.ashx?s=" + this.SessionKey + "&c=" + this.ClientID;
					row.Cells[0].Controls.Add(l11);

					Literal l12 = new Literal();
					l12.Text = ". ";
					row.Cells[0].Controls.Add(l12);
				}
			}

			return numRows;
		}

		protected void OnClick_lbPageSize2(object sender, EventArgs e)
		{
			this.Save();

			int nPageSize = Convert.ToInt32(((TextBox)this.InnerTable.Parent.FindControl("txtPageSize2")).Text);

			if (nPageSize != -1)
			{
				this.AllowPaging = true;
				this.PageSize = nPageSize;
				this.DataSource = GetDataSource(true);
				this.DataBind();
			}
			else
			{
				this.AllowPaging = false;
				this.PageSize = nPageSize;
				this.DataSource = GetDataSource(true);
				this.DataBind();
			}

			if (m_lbPageSize != null)
				ScriptManager.GetCurrent(this.Page).SetFocus(m_lbPageSize);
		}

		protected void OnClick_lbEdit(object sender, EventArgs e)
		{
			this.Save();

			this.EnableEdit = !this.EnableEdit;
			if (CurrentlyEditedRowsIndexes != null)
			{
				CurrentlyEditedRowsIndexes.Clear();
				if (EnableEdit)
					for (int i = 0; i < this.Rows.Count; ++i)
						CurrentlyEditedRowsIndexes.Add(i);
			}
			this.DataBind();
			if (m_lbEdit != null)
				ScriptManager.GetCurrent(this.Page).SetFocus(m_lbEdit);
		}

		protected void OnClick_lbInsert(object sender, EventArgs e)
		{
			this.Save();

			this.EnableInsert = !this.EnableInsert;
			this.DataBind();
			if (m_lbInsert != null)
				ScriptManager.GetCurrent(this.Page).SetFocus(m_lbInsert);
		}

		protected void OnClick_lbFilter2(object sender, EventArgs e)
		{
			this.Save();

			Table t = this.InnerTable;
			string strFilter = GetFilter(t.Rows[t.Rows.Count - 1].Cells[0].Controls[0].Controls[0], false);
			if (GridViewRowFilter != strFilter)
			{
				GridViewRowFilter = strFilter;
				GridViewRowFilterText = GetFilter(t.Rows[t.Rows.Count - 1].Cells[0].Controls[0].Controls[0], true);
				this.DataSource = GetDataSource(true);
				this.DataBind();
			}
			if (m_lbFilter != null)
				ScriptManager.GetCurrent(this.Page).SetFocus(m_lbFilter);
		}

		private Panel CreatePanelPaginare()
		{
			// Panel paginare
			Panel p1 = new Panel();
			p1.ID = "pnPageSize";
			p1.CssClass = "modalPopup";
			p1.Style.Add("display", "none");

			Table t1 = new Table();
			p1.Controls.Add(t1);

			// row[0]
			TableRow tr = new TableRow();
			t1.Rows.Add(tr);

			TableCell td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.HorizontalAlign = HorizontalAlign.Center;
			td.Text = "<u>" + Resources.GridView.NrLiniiPagina + "</u>:";

			// row[1]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;

			Literal l11 = new Literal();
			l11.Text = "<br />";
			td.Controls.Add(l11);

			TextBox tb1 = new TextBox();
			tb1.ID = "txtPageSize";
			tb1.Width = Unit.Pixel(100);
			tb1.Text = (this.AllowPaging ? this.PageSize.ToString() : "-1");
			td.Controls.Add(tb1);
			m_txtPageSize = tb1;

			HtmlInputButton inp1 = new HtmlInputButton();
			inp1.ID = "ButtonAllPages_PageSize";
			inp1.Value = Resources.GridView.AllLines;
			inp1.Attributes["AccessKey"] = "a";
			inp1.Attributes["class"] = "clsBtn";
			inp1.Style.Add("display", "none");
			td.Controls.Add(inp1);
			m_ButtonAllPages_PageSize = inp1;

			HtmlInputButton inp1_ = new HtmlInputButton();
			inp1_.ID = "ButtonAllPages_PageSize_";
			inp1_.Value = Resources.GridView.AllLines;
			inp1_.Attributes["AccessKey"] = "a";
			td.Controls.Add(inp1_);
			m_ButtonAllPages_PageSize_ = inp1_;

			// row[2]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.HorizontalAlign = HorizontalAlign.Center;

			Literal l12 = new Literal();
			l12.Text = "<br />";
			td.Controls.Add(l12);

			Button b1 = new Button();
			b1.ID = "OkButton_PageSize";
			b1.Text = "OK";
			b1.AccessKey = "f";
			b1.CssClass = "clsBtn";
			b1.Style.Add("display", "none");
			td.Controls.Add(b1);
			m_OkButton_PageSize = b1;

			Button b2 = new Button();
			b2.ID = "CancelButton_PageSize";
			b2.Text = Resources.GridView.Cancel;
			b2.AccessKey = "c";
			b2.CssClass = "clsBtn";
			b2.Style.Add("display", "none");
			td.Controls.Add(b2);
			m_CancelButton_PageSize = b2;

			HtmlInputButton b1_ = new HtmlInputButton();
			b1_.ID = "OkButton_PageSize_";
			b1_.Value = "OK";
			b1_.Attributes["AccessKey"] = "f";
			td.Controls.Add(b1_);
			m_OkButton_PageSize_ = b1_;

			HtmlInputButton b2_ = new HtmlInputButton();
			b2_.ID = "CancelButton_PageSize_";
			b2_.Value = Resources.GridView.Cancel;
			b2_.Attributes["AccessKey"] = "c";
			td.Controls.Add(b2_);
			m_CancelButton_PageSize_ = b2_;

			HtmlInputButton inp2 = new HtmlInputButton();
			inp2.ID = "ResetButton_PageSize";
			inp2.Value = Resources.GridView.Reset;
			inp2.Attributes["AccessKey"] = "r";
			inp2.Attributes["class"] = "clsBtn";
			inp2.Style.Add("display", "none");
			td.Controls.Add(inp2);
			m_ResetButton_PageSize = inp2;

			HtmlInputButton inp2_ = new HtmlInputButton();
			inp2_.ID = "ResetButton_PageSize_";
			inp2_.Value = Resources.GridView.Reset;
			inp2.Attributes["AccessKey"] = "r";
			td.Controls.Add(inp2_);
			m_ResetButton_PageSize_ = inp2_;

			return p1;
		}

		private Panel CreatePanelFiltrare(DataControlField[] fields)
		{
			// Panel filtrare
			Panel p1 = new Panel();
			p1.ID = "pnFilter";
			p1.CssClass = "modalPopup";
			p1.Style.Add("display", "none");

			Table t1 = new Table();
			p1.Controls.Add(t1);

			// row[0]
			TableRow tr = new TableRow();
			t1.Rows.Add(tr);

			TableCell td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.ColumnSpan = 2;
			td.HorizontalAlign = HorizontalAlign.Center;
			td.Text = "<u>" + Resources.GridView.FilterCriteria + "</u>:";

			// row[1]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;

			Table t2 = new Table();
			t2.ID = "tblFilter";
			td.Controls.Add(t2);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;

			Table t3 = new Table();
			t3.ID = "tblFilter2";
			td.Controls.Add(t3);

			// row[2]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.ColumnSpan = 2;

			Table t4 = new Table();
			td.Controls.Add(t4);
			t4.Width = Unit.Percentage(100);

			tr = new TableRow();
			if (!this.EnableOtherCriterias)
				tr.Style.Add("display", "none");
			t4.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.Text = Resources.GridView.OtherCriterias + ":";

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.Width = Unit.Percentage(100);

			TextBox tb1 = new TextBox();
			tb1.ID = "txtAlteCriterii";
			tb1.Width = Unit.Percentage(95);
			if (this.HtFilter[tb1.ID] != null)
				tb1.Text = this.HtFilter[tb1.ID].ToString();
			td.Controls.Add(tb1);

			// row[3]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.ColumnSpan = 2;

			Label l1 = new Label();
			l1.ID = "lblExplicatieData";
			l1.Text = Resources.GridView.DataFormatDescription;
			td.Controls.Add(l1);

			// row[4]
			tr = new TableRow();
			t1.Rows.Add(tr);

			td = new TableCell();
			tr.Cells.Add(td);
			td.Wrap = false;
			td.HorizontalAlign = HorizontalAlign.Center;

			Button b1 = new Button();
			b1.ID = "OkButton_Filter";
			b1.Text = "OK";
			b1.AccessKey = "f";
			b1.CssClass = "clsBtn";
			b1.Style.Add("display", "none");
			td.Controls.Add(b1);
			m_OkButton_Filter = b1;

			Button b2 = new Button();
			b2.ID = "CancelButton_Filter";
			b2.Text = Resources.GridView.Cancel;
			b2.AccessKey = "c";
			b2.CssClass = "clsBtn";
			b2.Style.Add("display", "none");
			td.Controls.Add(b2);
			m_CancelButton_Filter = b2;

			HtmlInputButton b1_ = new HtmlInputButton();
			b1_.ID = "OkButton_Filter_";
			b1_.Value = "OK";
			b1_.Attributes["AccessKey"] = "f";
			td.Controls.Add(b1_);
			m_OkButton_Filter_ = b1_;

			HtmlInputButton b2_ = new HtmlInputButton();
			b2_.ID = "CancelButton_Filter_";
			b2_.Value = Resources.GridView.Cancel;
			b2_.Attributes["AccessKey"] = "c";
			td.Controls.Add(b2_);
			m_CancelButton_Filter_ = b2_;

			HtmlInputButton inp2 = new HtmlInputButton();
			inp2.ID = "ResetButton_Filter";
			inp2.Value = Resources.GridView.Reset;
			inp2.Attributes["AccessKey"] = "r";
			inp2.Attributes["onclick"] = "OnClick_ResetButton_Filter('" + this.ClientID + "');";
			inp2.Attributes["class"] = "clsBtn";
			inp2.Style.Add("display", "none");
			td.Controls.Add(inp2);
			m_ResetButton_Filter = inp2;

			HtmlInputButton inp2_ = new HtmlInputButton();
			inp2_.ID = "ResetButton_Filter_";
			inp2_.Value = Resources.GridView.Reset;
			inp2_.Attributes["AccessKey"] = "r";
			td.Controls.Add(inp2_);
			m_ResetButton_Filter_ = inp2_;

			BuildFilterTable(t2, t3, l1, fields);

			return p1;
		}

		private object[] GetFilterArray(DataControlField[] fields)
		{
			if (this.DataSource == null)
				return new object[0];
			object[] o = new object[fields.Length];
			int i = 0;
			DataTable dt = this.DataSource.Table;
			foreach (DataControlField dcfc in fields)
			{
				string strCaption = "";
				int nOrdinal = -1;
				Type tDataType = null;
				if (dcfc is CheckBoxField)
				{
					strCaption = ((CheckBoxField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((CheckBoxField)dcfc).DataField].Ordinal;
					tDataType = dt.Columns[((CheckBoxField)dcfc).DataField].DataType;
				}
				else if (dcfc is HyperLinkField)
				{
					strCaption = ((HyperLinkField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((HyperLinkField)dcfc).DataTextField].Ordinal;
					tDataType = dt.Columns[((HyperLinkField)dcfc).DataTextField].DataType;
				}
				else if (dcfc is TextBoxField)
				{
					strCaption = ((TextBoxField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((TextBoxField)dcfc).DataField].Ordinal;
					tDataType = dt.Columns[((TextBoxField)dcfc).DataField].DataType;
				}
				else if (dcfc is DropDownField)
				{
					strCaption = ((DropDownField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((DropDownField)dcfc).DataField].Ordinal;
					tDataType = dt.Columns[((DropDownField)dcfc).DataField].DataType;
				}
				else if (dcfc is BoundField)
				{
					strCaption = ((BoundField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((BoundField)dcfc).DataField].Ordinal;
					tDataType = dt.Columns[((BoundField)dcfc).DataField].DataType;
				}
				else if (dcfc is AutoGeneratedField)
				{
					strCaption = ((AutoGeneratedField)dcfc).HeaderText;
					nOrdinal = dt.Columns[((AutoGeneratedField)dcfc).DataField].Ordinal;
					tDataType = dt.Columns[((AutoGeneratedField)dcfc).DataField].DataType;
				}
				o[i] = new object[3];
				((object[])o[i])[0] = strCaption;
				((object[])o[i])[1] = nOrdinal;
				((object[])o[i])[2] = tDataType;
				++i;
			}
			return o;
		}

		private void BuildFilterTable(Table tblFilter, Table tblFilter2, Label lblExplicatieData, DataControlField[] fields)
		{
			int i = 0;
			bool bDate = false;
			tblFilter.Rows.Clear();
			tblFilter2.Rows.Clear();
			object[] oo = GetFilterArray(fields);
			Hashtable htFilter = this.HtFilter;
			int n = oo.Length;
			foreach (object[] o in oo)
			{
				string strCaption = (string)o[0];
				int nOrdinal = (int)o[1];
				Type tDataType = (Type)o[2];
				if (nOrdinal == -1 || strCaption == "")
					continue;

				TableRow tr = new TableRow();

				TableHeaderCell th = new TableHeaderCell();
				th.HorizontalAlign = HorizontalAlign.Left;
				th.Wrap = false;
				Literal lName = new Literal();
				lName.Text = strCaption;
				th.Controls.Add(lName);
				tr.Cells.Add(th);

				TableCell td = new TableCell();
				td.Wrap = false;
				Literal lOp = new Literal();
				lOp.Text = Resources.GridView.equal;
				td.Controls.Add(lOp);
				tr.Cells.Add(td);

				td = new TableCell();
				td.Wrap = false;
				TextBox t = new TextBox();
				t.ID = "txtFilter_from_" + nOrdinal.ToString();
				t.Width = 50;
				if (htFilter[t.ID] != null)
					t.Text = htFilter[t.ID].ToString();
				td.Controls.Add(t);
				tr.Cells.Add(td);

				if (tDataType == System.Type.GetType("System.DateTime") || tDataType == System.Type.GetType("System.Decimal") || tDataType == System.Type.GetType("System.Int32"))
				{
					tr.Cells[1].Text = Resources.GridView.From;

					td = new TableCell();
					td.Wrap = false;
					Literal lOp2 = new Literal();
					lOp2.Text = Resources.GridView.To;
					td.Controls.Add(lOp2);
					tr.Cells.Add(td);

					td = new TableCell();
					td.Wrap = false;
					TextBox t2 = new TextBox();
					t2.ID = "txtFilter_to_" + nOrdinal.ToString();
					t2.Width = 50;
					if (htFilter[t2.ID] != null)
						t2.Text = htFilter[t2.ID].ToString();
					td.Controls.Add(t2);
					tr.Cells.Add(td);
				}
				else
				{
					td.ColumnSpan = 3;
					t.Width = 100;
				}

				if (n > 25 && i > n / 2)
					tblFilter2.Rows.Add(tr);
				else
					tblFilter.Rows.Add(tr);
				++i;
				if (tDataType == System.Type.GetType("System.DateTime"))
					bDate = true;
			}
			lblExplicatieData.Text = (bDate ? Resources.GridView.DataFormatDescription : "<BR />");
		}

		private string GetFilter(Control cParent, bool bText)
		{
			string strFilter = "";
			ArrayList arFilter = new ArrayList();
			Hashtable htFilter = new Hashtable();
			DataTable dt = this.DataSource.Table;
			foreach (DataColumn dc in dt.Columns)
			{
				Control cFrom = cParent.FindControl("txtFilter_from_" + dc.Ordinal.ToString());
				Control cTo = cParent.FindControl("txtFilter_to_" + dc.Ordinal.ToString());
				if (cFrom != null)
				{
					if (cTo == null)
					{
						if (((TextBox)cFrom).Text != "")
						{
							arFilter.Add("([" + (bText ? GetDataFieldHeaderName2(dc.ColumnName, true) : dc.ColumnName) + "] LIKE '%" + ((TextBox)cFrom).Text + "%')");
							htFilter.Add(((TextBox)cFrom).ID, ((TextBox)cFrom).Text);
						}
					}
					else
					{
						if (dc.DataType == System.Type.GetType("System.DateTime"))
						{
							if (((TextBox)cFrom).Text != "")
							{
								arFilter.Add("([" + (bText ? GetDataFieldHeaderName2(dc.ColumnName, true) : dc.ColumnName) + "] >= '" + ((TextBox)cFrom).Text + "')");
								htFilter.Add(((TextBox)cFrom).ID, ((TextBox)cFrom).Text);
							}
							if (((TextBox)cTo).Text != "")
							{
								arFilter.Add("([" + (bText ? GetDataFieldHeaderName2(dc.ColumnName, true) : dc.ColumnName) + "] <= '" + ((TextBox)cTo).Text + "')");
								htFilter.Add(((TextBox)cTo).ID, ((TextBox)cTo).Text);
							}
						}
						else
						{
							if (((TextBox)cFrom).Text != "")
							{
								arFilter.Add("([" + (bText ? GetDataFieldHeaderName2(dc.ColumnName, true) : dc.ColumnName) + "] >= " + ((TextBox)cFrom).Text + ")");
								htFilter.Add(((TextBox)cFrom).ID, ((TextBox)cFrom).Text);
							}
							if (((TextBox)cTo).Text != "")
							{
								arFilter.Add("([" + (bText ? GetDataFieldHeaderName2(dc.ColumnName, true) : dc.ColumnName) + "] <= " + ((TextBox)cTo).Text + ")");
								htFilter.Add(((TextBox)cTo).ID, ((TextBox)cTo).Text);
							}
						}
					}
				}
			}
			TextBox txtAlteCriterii = (TextBox)cParent.FindControl("txtAlteCriterii");
			if (txtAlteCriterii.Text != "")
			{
				arFilter.Add("(" + txtAlteCriterii.Text + ")");
				htFilter.Add(((TextBox)txtAlteCriterii).ID, ((TextBox)txtAlteCriterii).Text);
			}
			this.HtFilter = htFilter;
			string[] ss = new string[arFilter.Count];
			arFilter.CopyTo(ss);
			strFilter = string.Join(" AND ", ss);
			return strFilter;
		}
	}
}